package com.github.tetrisgame.ui.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.github.tetrisgame.R;
import com.github.tetrisgame.utils.UIUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class HeadView extends View {
    private int mSize = UIUtils.dp2px(100);
    private float shadowSize = UIUtils.dp2px(15);
    private int mShadowRadius = UIUtils.dp2px(6);
    private RectF rectF = new RectF();
    private Paint mPaint = new Paint();
    private Paint mShadowPaint = new Paint();
    private int mOuterColor = Color.parseColor("#F8F4E3");
    Paint mHeadOuterPaint = new Paint();
    private Bitmap bitmap;
    private Uri imageUri;
    private int mProgressColor;
    private String mText;
    private float mTextColor;
    private float mTextSize;
    Path roundCanvasPath = new Path();
    Path roundOuterCanvasPath = new Path();
    public HeadView(Context context) {
        this(context,null);
    }

    public HeadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public HeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs,defStyleAttr);
        init();
        float defaultSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,14,context.getResources().getDisplayMetrics());

        //从attrs中解析自己定义的属性的值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeadView);
        //解析文本 尺寸 颜色
        mOuterColor = typedArray.getColor(R.styleable.HeadView_backgroundCircleColor,Color.WHITE);
        mProgressColor = typedArray.getColor(R.styleable.HeadView_progressColor,Color.WHITE);
        mText = typedArray.getString(R.styleable.HeadView_text);
        mTextColor = typedArray.getColor(R.styleable.HeadView_textColor,Color.WHITE);
        shadowSize = typedArray.getDimension(R.styleable.HeadView_shadowSize,defaultSize);
        mTextSize = typedArray.getDimension(R.styleable.HeadView_textSize,defaultSize);
        //释放内存
        typedArray.recycle();
        mHeadOuterPaint.setColor(mOuterColor);
    }
    private void init(){
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);
        BlurMaskFilter blurFilter = new BlurMaskFilter(mShadowRadius, BlurMaskFilter.Blur.NORMAL);
        mShadowPaint.setMaskFilter(blurFilter);
        mShadowPaint.setShadowLayer(mShadowRadius,0,0,Color.parseColor("#80000000"));
        mShadowPaint.setColor(Color.parseColor("#FFFFFF"));
        mHeadOuterPaint.setColor(mOuterColor);
        mHeadOuterPaint.setStyle(Paint.Style.STROKE);
        mHeadOuterPaint.setStrokeWidth(UIUtils.dp2px(3));

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        roundCanvasPath.addRoundRect(
                shadowSize,
                shadowSize,
                getWidth()-shadowSize,
                getHeight()-shadowSize,

                9999,
                9999,
                Path.Direction.CW

        );
        roundOuterCanvasPath.addRoundRect(
                0,
                0,
                getWidth(),
                getHeight(),
                9999,
                9999,
                Path.Direction.CW
        );


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int desireWidth = mSize;
        int desireHeight = mSize;
        int realWidth = resolveSizeAndState(desireWidth,widthMeasureSpec,MEASURED_STATE_TOO_SMALL);
        int realHeight = realWidth;
        setMeasuredDimension(realWidth,realWidth);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {

        canvas.save();
        canvas.clipPath(roundOuterCanvasPath);
        drawShadow(canvas);
        canvas.restore();
        canvas.save();
        canvas.clipPath(roundCanvasPath);
        drawHead(canvas);
        drawHeadOuter(canvas);
        canvas.restore();
        


    }

    private void drawHeadOuter(Canvas canvas) {
        rectF.left = shadowSize;
        rectF.top = shadowSize;
        rectF.right = getWidth() - shadowSize;
        rectF.bottom = getHeight() - shadowSize;


        canvas.drawCircle(getWidth()/2,getHeight()/2,(getWidth()-2*shadowSize)/2,mHeadOuterPaint);
    }

    private void drawShadow(Canvas canvas) {
        rectF.left = shadowSize;
        rectF.top = shadowSize;
        rectF.right = getWidth()-shadowSize;
        rectF.bottom = getHeight()-shadowSize;
        canvas.drawCircle(getWidth()/2,getHeight()/2,(getWidth()-2*shadowSize)/2,mShadowPaint);
    }

    public void setImageBitmap(Bitmap bitmap){
        this.bitmap = bitmap;
        invalidate();
    }
    public void setImageUri(Uri imageUri){
        this.imageUri = imageUri;

    }

    private void drawHead(Canvas canvas) {
        rectF.left = shadowSize;
        rectF.top = shadowSize;
        rectF.right = getWidth() - shadowSize;
        rectF.bottom = getHeight() - shadowSize;

        canvas.drawBitmap(bitmap,null,rectF,mPaint);
    }
}
